/**
 * JAVACC DEMO 1.0
 */
package com.apache.audit.controller;

import com.apache.action.ActionSupport;
import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.api.vo.Token;
import com.apache.audit.entity.LogModelDetail;
import com.apache.audit.manager.LogModelDetailManager;
import com.apache.database.constant.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * description:  模型属性管理Action
 *
 * @author iussoft 创建时间：2016-4-9
 */
@Controller
@RequestMapping("/audit/modeldetail/")
public class LogModelDetailAction extends ActionSupport<LogModelDetail> {

    @Autowired
    private LogModelDetailManager logModelDetailManager;

    /**
     * 功能说明：通用跳转到列表查询页面
     */
    @RequestMapping("list.action")
    public ModelAndView list(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(LIST);
        view.addObject("modelId", request.getParameter("modelId"));
        return view;
    }

    /**
     * TODO ajax日志模型配置列表.
     */
    @RequestMapping("data.action")
    @ResponseBody
    protected Object data(HttpServletRequest request, String arg1, String arg2) {
        ParamsVo<LogModelDetail> vo = new ParamsVo<LogModelDetail>();
        LogModelDetail model = new LogModelDetail();
        if (Validator.isNotNull(request.getParameter("modelId"))) {
            model.setModelId(request.getParameter("modelId"));
        }
        vo.setObj(model);
        List<LogModelDetail> list = logModelDetailManager.getList(vo);
        return list;
    }

    /**
     * 功能说明：跳转到新增页面
     */
    @Token(save = true)
    @RequestMapping(value = "add.action", method = RequestMethod.GET)
    protected ModelAndView add(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(ADD);
        LogModelDetail model = new LogModelDetail();
        if (Validator.isNotNull(request.getParameter("id"))) {
            ParamsVo<LogModelDetail> vo = new ParamsVo<LogModelDetail>();
            vo.setInfoId(request.getParameter("id"));
            model = (LogModelDetail) logModelDetailManager.getInfoById(vo);
        } else {
            model.setModelId(request.getParameter("modelId"));
        }
        view.addObject("item", model);
        return view;
    }

    /**
     * 功能说明：通用保存页面
     */
    @Token(remove = true)
    @RequestMapping(value = "/save.action", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg save(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute LogModelDetail item) {
        ResultMsg rmsg = new ResultMsg("T", "操作成功！");
        ParamsVo<LogModelDetail> vo = new ParamsVo<LogModelDetail>();
        if (Validator.isNotNull(item.getDetailId())) {//对信息修改
            vo.setObj(item);
            boolean mark = getBaseManager().editInfo(vo);
            if (!mark) {
                rmsg = getResultMsg(request, "信息修改失败！");
            }
        } else {//添加信息
            vo.setObj(item);
            String id = getBaseManager().saveInfo(vo);
            if (Validator.isNull(id)) {
                rmsg = getResultMsg(request, "信息添加失败！");
            }
        }
        return rmsg;
    }

    /**
     * 功能说明：删除功能
     *
     * @param id 删除的id
     */
    @RequestMapping(value = "/del.action")
    @ResponseBody
    public ResultMsg delete(HttpServletRequest request, String id) {
        ResultMsg rmsg = new ResultMsg("T", "删除成功！");
        ParamsVo<LogModelDetail> vo = new ParamsVo<LogModelDetail>();
        if (isNotNull(id)) {
            vo.setInfoId(id);
            boolean mark = getBaseManager().deleteInfo(vo);
            if (!mark) {
                rmsg.setFlag("F");
                rmsg.setMsg("删除失败！");
            }
        } else {
            rmsg.setFlag("H");
            rmsg.setMsg("请不要恶意操作！");
        }
        return rmsg;
    }

    protected BaseManager<LogModelDetail> getBaseManager() {
        return logModelDetailManager;
    }

    protected String getPrefix() {
        return "audit/log-modeldetail-";
    }

}
